package mvc.struts.annotation.helper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import mvc.struts.annotation.util.ReflectionUtil;


public final class BeanHelper {

	private static final Map<Class<?>, Object> Bean_Map = new HashMap<Class<?>, Object>();
	
	static 
	{
		Set<Class<?>> 	beanClassSet = ClassHelper.getBeanClassSet();
		for	(Class<?>	beanClass : beanClassSet)
		{
			Object	obj = ReflectionUtil.newInstance(beanClass);
			Bean_Map.put(beanClass, obj);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static Map<Class<?>, Object>	getBeanMap()
	{
		return	Bean_Map;
	}
	
	/**
	 * 
	 * @param cls
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> cls)
	{
		if	(!Bean_Map.containsKey(cls))
		{
			throw	new RuntimeException("can not get instance" + cls);
		}
		
		return	(T)Bean_Map.get(cls);
		
	}
}
